package com.cloudweb.oa.controller;

import cn.js.fan.util.DateUtil;
import com.cloudweb.oa.cache.UserCache;
import com.cloudweb.oa.entity.UserSetup;
import com.cloudweb.oa.service.IUserService;
import com.cloudweb.oa.service.IUserSetupService;
import com.cloudweb.oa.service.LoginService;
import com.cloudweb.oa.vo.Result;
import io.swagger.annotations.*;
import org.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

@Api(tags = "登录模块")
@Controller
@RequestMapping("/")
public class LoginController {
    @Autowired
    HttpServletRequest request;

    @Autowired
    LoginService loginService;

    @Autowired
    IUserSetupService userSetupService;

    @Autowired
    IUserService userService;

    @ResponseBody
    @RequestMapping(value = "/doLogin", method = RequestMethod.POST, produces = {"text/html;charset=UTF-8;", "application/json;"})
    public String doLogin(HttpServletResponse response) throws JSONException {
        // 会被spring security拦截，故不用实现doLogin方法
        // return loginService.login(request, response);
        return "";
    }

    @ApiOperation(value = "强制退出", notes = "强制退出", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userNames", value = "用户名，以逗号分隔", required = true, dataType = "String"),
    })
    @ResponseBody
    @RequestMapping(value = "/admin/forceLogout", method = RequestMethod.POST, produces = {"text/html;charset=UTF-8;", "application/json;"})
    @ApiResponses({ @ApiResponse(code = 200, message = "操作成功") })
    public Result<Object> forceLogout(@RequestParam String userNames) {
        for (String userName : userNames.split(",")) {
            userService.incrementTokenVersion(userName);
        }
        return new Result<>();
    }
}
